package com.heimdallr.util;

import com.heimdallr.rbac.permission.entity.MenuEntity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author user
 */
public class AppUtils {

	private final static Log log = LogFactory.getLog(AppUtils.class);

    public enum ProfileEnum {
        dev,prod,prod_slave,local
    }

    /**
     * // 进行加法运算
     *
     * @param d1
     * @param d2
     * @return
     */
    public static double add(double d1, double d2) {

        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.add(b2).doubleValue();
    }

    /**
     * // 进行减法运算
     *
     * @param d1
     * @param d2
     * @return
     */
    public static double sub(double d1, double d2) {

        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.subtract(b2).doubleValue();
    }

    /**
	 * 进行乘法运算
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static double mul(double d1, double d2) { 
		
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.multiply(b2).doubleValue();
	}
	/**
	 *  进行除法运算
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static double div(double d1, double d2, int len) {
		
		if(d2==0){
			return 0;
		}
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
	}

    public static ProfileEnum getProfile(){

        //支持动态改
        String profile = System.getProperty("spring.profiles.active");

        if(isNull(profile)){
            return ProfileEnum.dev;
        }else if(profile.equals("dev")){
            return ProfileEnum.dev;
        }else if(profile.equals("prod")){
            return ProfileEnum.prod;
        }else if(profile.equals("prod-slave")){
            return ProfileEnum.prod_slave;
        }else if(profile.equals("local")){
            return ProfileEnum.local;
        }else{
            return ProfileEnum.local;
        }
    }

	/*** 
     * MD5加密 生成32位md5码
     * @return 返回32位md5码
     */
    public static String md5Encode(String inStr) throws Exception {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) { e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
		for (byte md5Byte : md5Bytes) {
			int val = ((int) md5Byte) & 0xff;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}
        return hexValue.toString();
	
	
    }

	public static boolean isMobile(String userAgent) {

		String[] mobileAgents = {"iphone", "android", "phone", "mobile", "wap", "netfront", "java",
				"opera mobi", "opera mini", "ucweb", "windows ce", "symbian", "series", "webos",
				"sony", "blackberry", "dopod", "nokia", "samsung", "palmsource", "xda", "pieplus",
				"meizu", "midp", "cldc", "motorola", "foma", "docomo", "up.browser", "up.link",
				"blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", "techfaith",
				"palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem",
				"wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-",
				"longcos", "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier",
				"^lct", "320x320", "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi",
				"avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco",
				"eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d",
				"lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp",
				"nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox",
				"qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-",
				"siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tsm-", "upg1",
				"upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw",
				"xda", "xda-", "Googlebot-Mobile","ipad"};

		if (isNotNull(userAgent)) {
			for (String mobileAgent : mobileAgents) {
				if (userAgent.toLowerCase().contains(mobileAgent)) {
					System.out.println("-----移动设备类型------" + mobileAgent + "-----------------------------");
					return true;
				}
			}
		}

		return false;
	}

    public static boolean isNotNull(String str) {
        return str != null && !"null".equals(str.trim()) && !"".equals(str.trim());
    }

    public static boolean isNull(String str) {
        return str == null || "null".equals(str.trim()) || "".equals(str.trim());
    }

	/**
	 * 获取菜单孩子节点
	 * @param allMenu
	 * @param menu
	 * @return
	 */
	public static List<MenuEntity> getChildren(List<MenuEntity> allMenu, MenuEntity menu){
		return allMenu.stream().filter(entity -> entity.getPid()!=null&&entity.getPid().intValue() == menu.getId().intValue()).collect(Collectors.toList());
	}

	/**
	 * 构建菜单树
	 * @param allMenu
	 * @param menu
	 */
	public static void buildTree(List<MenuEntity> allMenu,MenuEntity menu){
		List<MenuEntity> children = getChildren(allMenu,menu);
		if(children!=null&&!children.isEmpty()){
			for(MenuEntity entity:children){
                buildTree(allMenu,entity);
			}
			menu.setChildren(children);
		}
	}

}
 
